<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/stores.js'
import { getNavTree } from '@/apis/apis.js'

const userStore = useUserStore()
const router = useRouter()
const data = ref([])

const handleLogout = () => {
  userStore.deleteUser()
  router.push('/login')
}

onMounted(async () => {
  const res = await getNavTree()
  data.value = res.data
})
</script>

<template>
  <el-container>
    <el-header>
      <div class="header-left" @click="$router.push('/home')">
        <el-icon size="30">
          <MostlyCloudy />
        </el-icon>
        <div>后台管理系统</div>
      </div>
      <div class="header-right">
        <div class="user">
          <div class="user-text">{{ userStore.user.name }}</div>
          <el-icon color="#F56C6C" size="18px">
            <Bell />
          </el-icon>
        </div>
        <div class="logout" @click="handleLogout">退出</div>
      </div>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <el-scrollbar class="aside-scrollbar">
          <el-menu router :default-active="$route.path">
            <template v-for="item in data" :key="item.id">
              <el-menu-item v-if="item.children === null" :index="item.path">
                <el-icon>
                  <component :is="item.icon" />
                </el-icon>
                <span>{{ item.name }}</span>
              </el-menu-item>
              <el-sub-menu v-else :index="item.path">
                <template #title>
                  <el-icon>
                    <component :is="item.icon" />
                  </el-icon>
                  <span>{{ item.name }}</span>
                </template>
                <el-menu-item v-for="children in item.children" :key="children.id" :index="children.path">{{ children.name }}</el-menu-item>
              </el-sub-menu>
            </template>
          </el-menu>
        </el-scrollbar>
      </el-aside>
      <el-main>
        <el-scrollbar class="main-scrollbar">
          <router-view></router-view>
        </el-scrollbar>
      </el-main>
    </el-container>
  </el-container>
</template>

<style scoped lang="scss">
.el-container {
  height: 100vh;

  .el-header {
    display: flex;
    justify-content: space-between;
    --el-header-padding: 0;
    background-color: #333841;
    .header-left {
      display: flex;
      align-items: center;
      width: 200px;
      color: #545c65;
      font-size: 18px;
      font-weight: bold;
      cursor: pointer;
      .el-icon {
        margin: 0 10px;
      }
    }
    .header-right {
      display: flex;
      .user {
        display: flex;
        align-items: center;
        .user-text {
          color: #fff;
        }
        .el-icon {
          margin: 0 5px;
        }
      }
      .logout {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 85px;
        background-color: #545c65;
        color: #fff;
        cursor: pointer;
      }
    }
  }

  .el-aside {
    background-color: #545c65;

    .aside-scrollbar {
      height: calc(100vh - 60px);
      .el-menu {
        --el-menu-bg-color: transparent;
        --el-menu-text-color: white;
        --el-menu-active-color: #E6A23C;
        --el-menu-hover-bg-color: #333841;
      }
    }
  }

  .main-scrollbar {
    height: calc(100vh - 100px);
  }
}
</style>
